package me.passtime.projt.fee;

import android.content.Intent;
import me.passtime.projt.http.AbstractHttpController;
import me.passtime.projt.http.UriConstants;
import me.passtime.projt.util.ToastUtils;

import java.text.DecimalFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-17
 * Time: 下午5:41
 */
public class IvrController extends AbstractHttpController<String> {
    private PaymentPage paymentPage;
    private PaymentConfirmPage confirmPage;
    public String cardNo;
    public String password;
    private String step = "0";
    private Double amount;
    private String phoneNumber;

    public IvrController(PaymentPage paymentPage) {
        super(String.class);
        this.paymentPage = paymentPage;
    }

    public IvrController(PaymentConfirmPage confirmPage) {
        super(String.class);
        this.confirmPage = confirmPage;
        this.step = "1";
    }

    public void checkIsIVRCard(String phoneNumber, String cardNo, String cardPwd, Double amount) {
        this.cardNo = cardNo;
        this.password = cardPwd;
        this.amount = amount;
        this.phoneNumber = phoneNumber;
        String postDate = "UserID=" + phoneNumber + "&MerID=629&UserPID=&AccName=&TransType=32";
        this.sendRequest(UriConstants.IVR, postDate);
    }

    @Override
    public void onRequestFinish(String response) {
        confirmPage.hideProgressDialog();
        if (response != null) {
            if (step == "0") {
                paymentPage.hideProgressDialog();
                String retCode = getServerResponseCode(response);
                if ("00".equals(retCode)) {//IVR注册用户
                    Intent intent = new Intent(paymentPage, PaymentConfirmPage.class);
                    intent.putExtra("amount", amount);
                    intent.putExtra("phoneNumber", phoneNumber);
                    intent.putExtra("orderId", "123");//todo
                    intent.putExtra("payPkg", "PayPkg");//todo
                    intent.putExtra("isIvrPay", true);
                    paymentPage.startActivity(intent);
                } else {
                    paymentPage.doUnionPay();
                }
            } else if (step == "1") {
                confirmPage.hideProgressDialog();
                if ("E_00".equals(response)) {//IVR成功
                    Intent intent = new Intent(confirmPage, PaymentSuccessPage.class);
                    intent.putExtra("amount", amount);
                    intent.putExtra("phoneNumber", phoneNumber);
                    confirmPage.startActivity(intent);
                }else{
                    ToastUtils.showMessage(confirmPage,"支付失败");
                }
            }
        }
    }


    public void payUserIvrInterface(String phoneNumber, String cardNo, String cardPwd, Double amount) {
        this.phoneNumber = phoneNumber;
        this.cardNo = cardNo;
        this.password = cardPwd;
        this.amount = amount;
        String postData = buildPostDate();
        this.sendRequest(UriConstants.IVR, postData);
    }


    private String buildPostDate() {
        DecimalFormat df = new DecimalFormat("#.00");
        String postData = "UserID=" + phoneNumber + "&MerID=629&PayID=123456&UserPID=&AccName=&BankCard=&&Bankpid=123456789012345&OrderAmount=" + df.format(amount) + "&Remark=123456789ABCEDF&TransType=15";
        return postData;
    }


    private String getServerResponseCode(String response) {
        String retCode = "";
        for (String s : response.split("&")) {
            if (s.startsWith("RetCode")) {
                retCode = s.split("=")[0];
                break;
            }
        }
        return retCode;
    }

    @Override
    public void onNetworkAbort() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，网络连接被中断.");
    }

    @Override
    public void onNetworkError() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，网络连接错误.");
    }

    @Override
    public void onResponseError() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，返回结果错误.");
    }
}
